<?php 
namespace Home\Model;
use Think\Model;
class UserModel extends Model
{
	protected $_validate = array(
			['username','/^[a-zA-Z][a-zA-Z0-9_]{2,10}$/','账号不符合要求！'],
			['pass','/^\w{6,20}$/','密码不符合要求！',3],
			['repass','pass','密码不一致哦！',1,'confirm',3],
			['email','/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/','邮箱不符合要求'],
			['email','','邮箱已经存在！',0,'unique',1 ],
		);

	protected $_auto=[
			// 新增的时候把status字段设置为1
			['status','1'], 
			// 对update_time字段在更新的时候写入当前时间戳
			['addtime','time',3,'function'],
			//密码通过password_hash加密 
			['pass','myHash',3,'function'],
		];

	/**
	 * [queryUser description]
	 * @return 验证用户输入的字段是否唯一及合法
	 */
	public function queryUser()
	{
		$user = I('post.user');
		
		$regUser = '/^[a-zA-Z][a-zA-Z0-9_]{2,10}$/';

		if(!preg_match($regUser, $user)){
			return "sign";
		}
		$map['username'] = ['eq',$user];
		$info = $this->where($map)->find();

		return $info['id'];
	}

	/**
	 * [queryEmail description]
	 * @return 验证用户输入的邮箱是否唯一及合法
	 */
	public function queryEmail()
	{
		$userEmail = I('post.userEmail');

		$regEmail = '/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/';

		if(!preg_match($regEmail, $userEmail)){
			return "sign";		
		}

		$map['email'] = ['eq',$userEmail];
		$info = $this->where($map)->find();
		return $info['id'];
	}

	/**
	 * [queryReg description]
	 * @return 对注册用户的字段进行插入数据库
	 */
	public function queryReg()
	{
		$post = I('post.');
		$res = $this->create($post);
		if($res){
				//如果符合判断条件添加到数据库
				$res=$this->add();
				 return $mag='注册成功！请重新登录';
			}else{
				//否则就提示错误信息
				 return $msg=$this->getError();
			}
	}

	/**
	 * [login 验证用户的用户的登录，成功则返回success到login控制器，
	 * 失败则返回error，用户名不存在则返non]
	 * @return [type] [success][error][non]
	 */
	public function login()
	{
		$username = I('post.username');
		$pass = I('post.pass');
		$map['username'] = ['eq',$username];
		$res = $this->where($map)->find();
		// return $res;
		if($res['id']>0){
			if(password_verify($pass,$res['pass'])){
				session("user",$res);	//存进session
				return "success";
			}else{
				return "error";
			}
		}else{
			return "non";
		}
	}

	/**
	 * [retpwd 检测用户在密码找回页面输入的用户名和邮箱进行合法性验证]
	 * @return [type] [description]
	 */
	public function retpwd()
	{
		$username = I('post.username');
		$email = I('post.email');
		$regUser = '/^[a-zA-Z][a-zA-Z0-9_]{2,10}$/';
		$regEmail = '/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/';
		
		if(!preg_match($regUser, $username)){
			return "signName";				//验证用户输入的密码是否合法
		}

		if(!preg_match($regEmail, $email)){
			return "signEmail";				//验证用户输入的邮箱是否合法
		}

		$map['username'] = $username;		//根据用户输入的用户名查找数据
		$list = $this->where($map)->find();
		// return $list;

		if($list['username']!=$username || $list['email']!=$email){		
			return 'error';					//检测用户的邮箱是否注册时所用的邮箱
		}

		//把用户的数据放进session方便在修改密码时根据用户名查找用户要修改的密码
		session('username',$list['username']);		
	}

	/**
	 * [tespwd 检测用户输入的密码是否合法用于ajax验证]
	 * @return [type] [description]
	 */
	public function tespwd()
	{
		$pass = I('post.');
		$regPwd = '/^\w{6,20}$/';
		if(!preg_match($regPwd, $pass['pass'])){
			return "non";		
		}
	}

	/**
	 * [modpws 对用户输入的密码进行修改操作]
	 * @return [type] [success修改成功]否则修改失败
	 */
	public function modpws()
	{
		$pass = I('post.');
		$map['username'] = session('username');

		$respwd = $this->create($pass,2);
		if($respwd){
			// 哈希加密
			$pass['pass'] = password_hash($pass['pass'],PASSWORD_DEFAULT); 
			
			$pwd = $this->where($map)->setField('pass',$pass['pass']);
			//session(null);	//用户修改密码成功后销毁session
			return 'success';
		}else{
			return $msg=$this->getError();
		}
	}
}